﻿package mortal.game.view.LineOffReward
{
    import flash.events.*;
    import mortal.common.*;
    import mortal.game.cache.*;
    import mortal.game.events.*;
    import mortal.game.mvc.*;
    import mortal.game.view.LineOffReward.data.*;
    import mortal.game.view.copy.QuestionCopy.data.*;
    import mortal.game.view.signUp.*;
    import mortal.game.view.signUp.util.*;
    import mortal.mvc.core.*;
    import mortal.mvc.interfaces.*;

    public class LineOffRewardController extends Controller
    {
        private var _lineOffModule:SignUpModule;
        private var _lineOffCache:LineOffRewardCache;
        private var _lineProxy:LineOffRewardProxy;
        private var _isFirst:Boolean = true;

        public function LineOffRewardController()
        {
            this._lineOffCache = Cache.instance.lineOff;
            this._lineProxy = GameProxy.lineOff;
            return;
        }// end function

        public function setView(param1:SignUpModule) : void
        {
            this._lineOffModule = param1;
            return;
        }// end function

        override protected function initView() : IView
        {
            return null;
        }// end function

        public function onShow(event:Event) : void
        {
            this.addEvents(true);
            return;
        }// end function

        public function onClose(event:Event) : void
        {
            this.addEvents(false);
            return;
        }// end function

        override protected function initServer() : void
        {
            EventUtil.addNetEvent(ServerCommand.LineOffRewardUpdateExp, this.onGotExpUpdata, true);
            EventUtil.addNetEvent(ServerCommand.LineOffRewardUpdateCopy, this.onGotCopyUpdate, true);
            EventUtil.addSelfEvent(EventName.NavbarButtonUIRowInited, this.initHd, true);
            EventUtil.addNetEvent(ServerCommand.LineOffRewardGodAllData, this.onInitDataHd, true);
            return;
        }// end function

        private function addEvents(param1:Boolean) : void
        {
            EventUtil.addSelfEvent(EventName.LineOffRewardOnOpenPage, this.queryData, param1);
            EventUtil.addSelfEvent(EventName.LineOffRewardGetExp, this.onGetExpHd, param1);
            EventUtil.addSelfEvent(EventName.LineOffRewardGetCopyOne, this.onGetCopyOneHd, param1);
            EventUtil.addSelfEvent(EventName.LineOffRewardOneKeyGetCopy, this.onOneKeyGetCopyHd, param1);
            return;
        }// end function

        private function initHd(param1:Object) : void
        {
            this.queryData(null);
            return;
        }// end function

        private function onGotExpUpdata(param1:Object) : void
        {
            if (this._lineOffModule && this._lineOffModule.lineOffExpPanel && this._lineOffModule.lineOffExpPanel.uiLoadComplete)
            {
                this._lineOffModule.lineOffExpPanel.updateExp();
            }
            return;
        }// end function

        private function onGotCopyUpdate(param1:Object) : void
        {
            var _loc_2:Array = null;
            var _loc_3:int = 0;
            if (this._lineOffModule && this._lineOffModule.lineOffCopyPanel && this._lineOffModule.lineOffCopyPanel.uiLoadComplete)
            {
                _loc_2 = param1 as Array;
                if (Cache.instance.lineOff.isOneKeyGet)
                {
                    this._lineOffModule.lineOffCopyPanel.updateAllDataProvider(_loc_2);
                    Cache.instance.lineOff.isOneKeyGet = false;
                }
                else
                {
                    _loc_3 = 0;
                    while (_loc_3 < _loc_2.length)
                    {
                        
                        this._lineOffModule.lineOffCopyPanel.updateOneCopy(_loc_2[_loc_3]);
                        _loc_3++;
                    }
                }
            }
            return;
        }// end function

        private function queryData(event:DataEvent) : void
        {
            this._lineProxy.getAllData();
            return;
        }// end function

        private function onInitDataHd(param1:Object) : void
        {
            if (this._lineOffModule && this._lineOffModule.lineOffCopyPanel && this._lineOffModule.lineOffCopyPanel.uiLoadComplete)
            {
                this._lineOffModule.lineOffCopyPanel.initCopy();
            }
            if (this._lineOffModule && this._lineOffModule.lineOffExpPanel && this._lineOffModule.lineOffExpPanel.uiLoadComplete)
            {
                this._lineOffModule.lineOffExpPanel.updateExp();
            }
            this.openExprentOrCopyPanel();
            return;
        }// end function

        private function openExprentOrCopyPanel() : void
        {
            if (this._isFirst == false)
            {
                return;
            }
            this._isFirst = false;
            if (Cache.instance.lineOff.lineOffInfo == null)
            {
                return;
            }
            if (Cache.instance.lineOff.lineOffInfo.offlineTime >= 12 * 3600 && LevelCheckUtil.isLevelEnougth(LevelCheckUtil.lineOffExp))
            {
                Dispatcher.dispatchEvent(new DataEvent(EventName.SignUpOpenWinAndSwitchTag, SignUpConst.Tag_OffLineExp));
            }
            else if (Cache.instance.lineOff.lineOffInfo.copysCompensation && Cache.instance.lineOff.lineOffInfo.copysCompensation.length > 0 && LevelCheckUtil.isLevelEnougth(LevelCheckUtil.lineOffCopy))
            {
                Dispatcher.dispatchEvent(new DataEvent(EventName.SignUpOpenWinAndSwitchTag, SignUpConst.Tag_OffLine));
            }
            return;
        }// end function

        private function onGetExpHd(event:DataEvent) : void
        {
            var _loc_2:* = event.data as int;
            this._lineProxy.getExpReward(_loc_2);
            return;
        }// end function

        private function onGetCopyOneHd(event:DataEvent) : void
        {
            var _loc_2:* = event.data;
            if (_loc_2)
            {
                this._lineProxy.getCopyOneReward(_loc_2.copyCode, _loc_2.type);
            }
            return;
        }// end function

        private function onOneKeyGetCopyHd(event:DataEvent) : void
        {
            var _loc_2:* = event.data as int;
            if (_loc_2 != 0)
            {
                Cache.instance.lineOff.isOneKeyGet = true;
                this._lineProxy.oneKeyGetCopyReward(_loc_2);
            }
            return;
        }// end function

        private function onExtremeGetHd(event:DataEvent) : void
        {
            var _loc_2:* = event.data as int;
            if (_loc_2 != 0)
            {
                this._lineProxy.oneKeyGetCopyReward(_loc_2);
            }
            return;
        }// end function

    }
}
